#!/bin/bash
# Start Thrivegrid. This starter never reads the drive itself: it asks Terminal to run
# Thrivegrid/tools/Start Thrivegrid.command. macOS ties its "Removable Volumes" permission to the app that
# reads the drive, and Terminal's permission is kept for good (an unsigned starter's is not).
ML="$HOME/Library/Logs/Thrivegrid"; mkdir -p "$ML" 2>/dev/null
note() { echo "$(date '+%Y-%m-%d %H:%M:%S') $*" >> "$ML/launcher.log" 2>/dev/null; }
TOP="$(cd "$(dirname "$0")/../../.." && pwd)"
C="Thrivegrid/tools/Start Thrivegrid.command"
# macOS can run a downloaded app from a hidden copy (App Translocation); then look for the drive itself.
if [ ! -e "$TOP/$C" ] || case "$TOP" in */AppTranslocation/*) true ;; *) false ;; esac; then
  for v in /Volumes/*; do [ -e "$v/$C" ] && { TOP="$v"; break; }; done
fi
case " $* " in *" --auto "*) : > "${TMPDIR:-/tmp}/thrivegrid-auto" ;; esac
note "start: $TOP"
if open -a Terminal "$TOP/$C" 2>>"$ML/launcher.log"; then exit 0; fi
note "Terminal could not open $TOP/$C"
osascript -e 'display alert "Thrivegrid" message "Start Thrivegrid could not open the Thrivegrid folder on this drive.\n\nCheck that the drive is plugged in and that the Thrivegrid folder sits next to Start Thrivegrid, then try again. If it still will not start, run Install or Update (Mac) from the Thrivegrid folder." as critical' >/dev/null 2>&1
exit 1
